from django.http import JsonResponse
import json

from salary_flow.models import Salary_list


# 分配器

def dispatcher(request):
    # 将请求同意放在request的params中
    print(request.body)
    if request.method == 'GET':
        request.params = request.GET
    elif request.method in ['POST', 'PUT', 'DELETE']:
        request.params = json.loads(request.body)

    # 根据不同的action分派给不同的函数进行处理
    action = request.params['action']
    if action == 'list_salary_list':
        return list_salary_list(request)
    elif action == 'add_salary_list':
        return add_salary_list(request)
    elif action == 'modify_salary_list':
        return modify_salary_list(request)
    elif action == 'del_salary_list':
        return del_salary_list(request)

    else:
        return JsonResponse({'ret': 1, 'msg': '不支持该类型http请求'}, json_dumps_params={'ensure_ascii': False})


def list_salary_list(request):
    # 返回一个 QuerySet 对象 ，包含所有的表记录
    qs = Salary_list.objects.values()

    # 将 QuerySet 对象 转化为 list 类型
    # 否则不能 被 转化为 JSON 字符串
    retlist = list(qs)

    return JsonResponse({'ret': 0, 'retlist': retlist}, json_dumps_params={'ensure_ascii': False})


def add_salary_list(request):
    print(request)
    info = request.params['data']

    # 从请求消息中 获取要添加部门的信息
    # 并且插入到数据库中
    # 返回值 就是对应插入记录的对象
    record = Salary_list.objects.create(SLid=info['id'],
                                  SLtitle=info['title'],
                                  SLcontent=info['content'],
                                  SLmonth=info['month'],
                                  SLoutteroffice=info['outteroffice'],
                                  SLinneroffice=info['inneroffice'],
                                  SLoutterlogin=info['outterlogin'],
                                  SLinnerlogin=info['innerlogin'],
                                  SLproject=info['project'],
                                  )

    return JsonResponse({'ret': 0, 'id': record.id}, json_dumps_params={'ensure_ascii': False})


def modify_salary_list(request):
    # 从请求消息中 获取修改部门的信息
    # 找到该部门，并且进行修改操作

    salary_list_id = request.params['id']
    newdata = request.params['newdata']

    try:
        # 根据 id 从数据库中找到相应的部门记录
        salary_list = Salary_list.objects.get(id=salary_list_id)
    except salary_list.DoesNotExist:
        return JsonResponse({
            'ret': 1,
            'msg': f'id 为`{salary_list_id}`的部门不存在'
        }, json_dumps_params={'ensure_ascii': False})

    if 'id' in newdata:
        salary_list.SLid = newdata['id']
    if 'title' in newdata:
        salary_list.SLtitle = newdata['title']
    if 'content' in newdata:
        salary_list.SLcontent = newdata['content']
    if 'month' in newdata:
        salary_list.SLmonth = newdata['month']
    if 'outteroffice' in newdata:
        salary_list.SLoutteroffice = newdata['outteroffice']
    if 'inneroffice' in newdata:
        salary_list.SLinneroffice = newdata['inneroffice']
    if 'outterlogin' in newdata:
        salary_list.SLoutterlogin = newdata['outterlogin']
    if 'innerlogin' in newdata:
        salary_list.SLinnerlogin = newdata['innerlogin']
    if 'project' in newdata:
        salary_list.SLproject = newdata['project']

    # 注意，一定要执行save才能将修改信息保存到数据库
    salary_list.save()

    return JsonResponse({'ret': 0}, json_dumps_params={'ensure_ascii': False})


def del_salary_list(request):
    salary_list_id = request.params['id']

    try:
        # 根据 id 从数据库中找到相应的部门记录
        salary_list = Salary_list.objects.get(id=salary_list_id)
    except salary_list.DoesNotExist:
        return JsonResponse({
            'ret': 1,
            'msg': f'id 为`{salary_list_id}`的部门不存在'
        }, json_dumps_params={'ensure_ascii': False})

    # delete 方法就将该记录从数据库中删除了
    salary_list.delete()

    return JsonResponse({'ret': 0}, json_dumps_params={'ensure_ascii': False})
